﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using ServiceStack.Redis;
using ServiceStack.Redis.Generic;

namespace RedisConsoleApp
{
    class Program
    {
        static readonly RedisClient client = new RedisClient("127.0.0.1", 6379);
        static readonly RedisClient SlaveRedis = new RedisClient("127.0.0.1", 6381);

        private static IRedisTypedClient<UserInfo> redis = client.As<UserInfo>();
        static void Main(string[] args)
        {
            //  StringTest();
            // HashTest();
            //QueueTest();
            // SetTest();
            // client.Password = "123456";
            // testc();
            //  SingleObjEnqueueTest();
            //  ListObjTest();

            QueueTest();
        }

        private static void testc()
        {
            client.Set("product","电脑");

         var result=   SlaveRedis.Get<string>("product");
            Console.Write(result);
            Console.ReadLine();
        }
        private static void StringTest()
        {
            client.Set("name222222222", "xiaoming");
            string userName = client.Get<string>("name222222222");
            Console.WriteLine(userName);
            UserInfo userInfo = new UserInfo() { UserName = "张三", UserPwd = "123455" };

            UserInfo userInfo2 = new UserInfo() { UserName = "xiahogn", UserPwd = "123455" };
            client.Set<UserInfo>("userinfo", userInfo);
            UserInfo userinfoTwo = client.Get<UserInfo>("userinfo");

            Console.WriteLine(userinfoTwo.UserName + "," + userinfoTwo.UserPwd);

            List<UserInfo> userInfos = new List<UserInfo>();
            userInfos.Add(userInfo);
            userInfos.Add(userInfo2);

            client.Set<List<UserInfo>>("userInfos", userInfos);
            List<UserInfo> userinfosTwo = client.Get<List<UserInfo>>("userInfos");
            foreach (var item in userinfosTwo)
            {
                Console.WriteLine(item.UserName + item.UserPwd);
            }

            Console.ReadLine();
        }

        private static void HashTest()
        {
            client.SetEntryInHash("userinfoId", "name", "zhangsan");
            var lstKeys = client.GetHashKeys("userinfoId");
            lstKeys.ForEach(s => Console.WriteLine(s));

            var lstValues = client.GetHashValues("userinfoId");
            lstValues.ForEach(v => Console.WriteLine(v));


            client.Remove("userinfoId");
            Console.ReadLine();
        }

        /// <summary>
        /// redis 队列
        /// </summary>
        private static void QueueTest()
        {
            client.EnqueueItemOnList("test", "xiaohogn");
            client.EnqueueItemOnList("test", "zhoujielun");

            long length = client.GetListCount("test");
            for (int i = 0; i < length; i++)//先进先出
            {
                Console.WriteLine(client.DequeueItemFromList("test"));
            }
            client.EnqueueItemOnList("test", "12345");
            client.EnqueueItemOnList("test", "67890");
            long length2 = client.GetListCount("test");
            for (int i = 0; i < length2; i++)//先进先出
            {
                Console.WriteLine(client.DequeueItemFromList("test"));
            }
            //client.PushItemToList("name1", "12344");
            //client.PushItemToList("name1", "33334546464");
            //long length1 = client.GetListCount("name1");
            //for (int i = 0; i < length1; i++)//后进先出
            //{
            //    Console.WriteLine(client.PopItemFromList("name1"));
            //}
            Console.ReadLine();
        }

        private static void SetTest()
        {
            client.AddItemToSet("highSchool", "11111");
            client.AddItemToSet("highSchool", "22222");
            client.AddItemToSet("highSchool", "333333");
            client.AddItemToSet("highSchool", "44444");
            client.AddItemToSet("highSchool", "熊平");
            client.AddItemToSet("highSchool", "66666");
            client.AddItemToSet("highSchool", "777777");
            var highSchool = client.GetAllItemsFromSet("highSchool");
            //gaozhong 同学

            ConsoleHashSetInfo(highSchool);

            //求并集   highSchool 和 college 合并查询  重复的不重复出现
            client.AddItemToSet("college", "邹琼俊");
            client.AddItemToSet("college", "卢沛");
            client.AddItemToSet("college", "熊平");
            client.AddItemToSet("college", "陈望");
            client.AddItemToSet("college", "王小敏");
            var hashSet2 = client.GetUnionFromSets(new string[] { "highSchool", "college" });

            Console.WriteLine("union 查询");
            ConsoleHashSetInfo(hashSet2);

            //求交集 highSchool 和 college 都有这个人
            var hashSet3 = client.GetIntersectFromSets(new string[] { "highSchool", "college" });
            Console.WriteLine("求交集 查询");
            ConsoleHashSetInfo(hashSet3);


            //求差集 查询college 没有 highSchool  的 highSchool 数据
            var hashSet4 = client.GetDifferencesFromSet("highSchool", new string[] { "college" });
            Console.WriteLine("求差集 查询");
            ConsoleHashSetInfo(hashSet4);

            Console.ReadLine();
        }

        private static void SortSetTest()
        {
            client.AddItemToSortedSet("highSchoo", "熊平", 9);
            client.AddItemToSortedSet("highSchoo", "熊平", 2);
            client.AddItemToSortedSet("highSchoo", "熊平", 3);
            client.AddItemToSortedSet("highSchoo", "熊平", 4);
            client.AddItemToSortedSet("highSchoo", "熊平", 5);
            client.AddItemToSortedSet("highSchoo", "66666", 6);
            client.AddItemToSortedSet("highSchoo", "777777", 7);
            //client.AddItemToSortedSet("friend", "熊平", 1);
            //client.AddItemToSortedSet("friend", "陈望", 3);
            //client.AddItemToSortedSet("friend", "王小敏", 5);
            //client.AddItemToSortedSet("friend", "刘继豪", 2);
            //client.AddItemToSortedSet("friend", "侯亮", 4);
            var list = client.GetAllItemsFromSortedSetDesc("highSchoo");
            foreach (var item in list)
            {
                Console.WriteLine(item);
            }

            Console.ReadLine();

        }

        private static void ConsoleHashSetInfo(HashSet<string> hs)
        {
            foreach (var str in hs)
            {
                if (str == "11111")
                {
                    continue;
                }
                Console.WriteLine(str);
            }

        }
        /// <summary>
        /// 6. 单个对象队列测试
        /// </summary>
        private static void SingleObjEnqueueTest()
        {
            UserInfo user = new UserInfo {UserName = "张三", UserPwd = "123"};
            JavaScriptSerializer json = new JavaScriptSerializer();
            client.EnqueueItemOnList("user",json.Serialize(user));
            user = json.Deserialize<UserInfo>(client.DequeueItemFromList("user"));
            Console.WriteLine(user.UserName+user.UserPwd);
            Console.ReadLine();
        }

        private static void ListObjTest()
        {
            List<UserInfo> list = new List<UserInfo>();
            UserInfo userInfo = new UserInfo() { UserName = "张三", UserPwd = "123455" };

            UserInfo userInfo2 = new UserInfo() { UserName = "xiahogn", UserPwd = "123455" };
            list.Add(userInfo);
            list.Add(userInfo2);
            AddInUserInfo(list);
            var rlist = redis.GetAllItemsFromList(redis.Lists["inUserList"]);
            rlist.ForEach(r=>Console.WriteLine(r.UserName+r.UserPwd));
            redis.RemoveAllFromList(redis.Lists["inUserList"]);
            Console.ReadLine();

        }

        private static void AddInUserInfo(List<UserInfo> list)
        {
            IRedisList<UserInfo> redisList = redis.Lists["inUserList"];
            redisList.AddRange(list);
        }



    }

    public class UserInfo
    {
        public string UserName { get; set; }
        public string UserPwd { get; set; }
    }
}
